var pattern = new RegExp(/\?OrderNo=(\S*)/g);
var idx = pattern.exec(window.location.href);

var Data02, Data04, Data05;

$(function(){
    requestAjax();
    gridInit02();
    $('li[data-opt="back"]').on('click', back)
    $('li[data-opt="u_check"]').on('click', saveCheck)
});

function requestAjax(){
    if (idx) {
        var _postData = Object.create(postData);    //请求头
        _postData.Method = 'Q';
        _postData.Content = {
            OrderNo: idx[1]
        }
        $.ajax({
            async: false,
            type: 'post',
            url: omsPath + '/OMS0012/OMS001203',
            data: _postData,
            dataType: 'json',
            success: function (re) {
                if (re.Status == 200) {
                    loadAjax(re);
                } else if (re.Status == -999) {
                    alert_QX($('body'), re.Message);
                } else {
                    alert(re.Message);
                }
            }
        });
    }
}

function loadAjax(re) {
    $('#CreateDate').val(re.Content.CreateDate);
    $('#UpdateDate').val(re.Content.UpdateDate);
    $('#DocStatus').val(re.Content.DocStatus);
    $('#UserCode').val(re.Content.UserCode);
    $('#UserName').val(re.Content.UserName);
    $('#DocTotal').val(re.Content.DocTotal);
    $('#AddressId').val(re.Content.AddressId);
    $('#Address').val(re.Content.Address);
    $('#Comment').val(re.Content.Comment);
    $('#OrderNo').val(re.Content.OrderNo);
    $('#Province').val(re.Content.Province);
    $('#Area').val(re.Content.Area);
    $('#City').val(re.Content.City);
    $('#Street').val(re.Content.Street);
    $('#Receiver').val(re.Content.Receiver);
    $('#ReceiverPhone').val(re.Content.ReceiverPhone);
    $('#CardCode').val(re.Content.CardCode);
    $('#CardName').val(re.Content.CardName);
    $('#TaxCompany').val(re.Content.TaxCompany);
    $('#TaxType').val(re.Content.TaxType);
    $('#TaxBank').val(re.Content.TaxBank);
    $('#TaxAccount').val(re.Content.TaxAccount);
    $('#TaxNumber').val(re.Content.TaxNumber);
    $('#TaxAddress').val(re.Content.TaxAddress);
    $('#TaxPhone').val(re.Content.TaxPhone);
    $('#Paymentmethod').val(re.Content.Paymentmethod);
    $('#PaymentStatus').val(re.Content.PaymentStatus);
    $('#LogisticStatus').val(re.Content.LogisticStatus);
    $('#TaxStatus').val(re.Content.TaxStatus);
    $('#ReqPayTotal').val(re.Content.ReqPayTotal);
    $('#PayTotal').val(re.Content.PayTotal);
    $('#PayChannel').val(re.Content.PayChannel);
    $('#Source').val(re.Content.Source);
    $('#SalesPerson').val(re.Content.SalesPerson);
    $('#ShoppingGuide').val(re.Content.ShoppingGuide);
    $('#CancelReason').val(re.Content.CancelReason);
    Data02 = re.Content.OMS001202;
    Data04 = re.Content.OMS001204;
    Data05 = re.Content.OMS001205;
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata) !== '{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function gridInit02(){
    var gridData02 = Object.create(gridData);
    gridData02.grid = $('#jgTable02');
    gridData02.datatype = "local";
    gridData02.rowNum = '100000';
    gridData02.colModel = [
        {label:'行号', name:'LineID', hidden:true},
        {label:'排序行号', name:'OrderById', hidden:true},
        {label:'订单编号', name:'OrderNo', hidden:true},
        {label:'SKU', name:'SKU'},
        {label:'物料描述', name:'ItemName'},
        {label:'基于类型', name:'BaseType'},
        {label:'核心规格', name:'Mainproperty'},
        {label:'价格', name:'Price'},
        {label:'数量', name:'Quantity'},
        {label:'折扣', name:'Discount'},
        {label:'行总计', name:'LineTotal'},
        {label:'行备注', name:'LineComment'},
        {label:'来源', name:'Source', hidden:true},
        {label:'创建时间', name:'CreateDate', hidden:true},
        {label:'更新时间', name:'UpdateDate', hidden:true},
        {label:'操作', name:'Options', hidden:true, width:60, formatter:alarmFormatter, sortable:false}
    ];
    gridData02.scroll = true;
    gridData02.height = '390px';
    gridData02.beforeRequest = function(){
        gridData02.grid.jqGrid("setGridParam", {
            data : Data02,
        });
    };
    gridData02.loadComplete = function(xhr){
        gridSize();
        // var rowData = gridData02.grid.jqGrid("getRowData");
        // $('a[opt-data="jgTable02_delete"]').unbind('click').bind('click',function(){
        //     var rowId = $(this).parent().parent()[0].id;
        //     gridData02.grid.jqGrid("delRowData",rowId);
        //     Data02.forEach(function(v,i,a){
        //         v.SKU===rowData[rowId-1].SKU&&Data02.splice(i,1);
        //     });
        //     console.log(Data02)
        // });
        // $('a[opt-data="jg_add"]').unbind('click').bind('click', addSKU);
    };
    gridInit(gridData02);
}


//Options
function back(){
    window.open('OMS001201.html', '_self');
}

function saveCheck(){
    var _requestData = {
        // Method: 'A'
    };
    for(i in postData){
        _requestData[i] = postData[i];
    };
    _requestData.Content = [
        {OrderNo: idx[1], UserCode: $('#UserCode').val()}
    ];
    console.log(_requestData)
    $.ajax({
        type: "post",
        url: omsPath+'/OMS0012/OMS001206',
        data: _requestData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                myAlert('审核成功');
                back();
            } else {
                myAlert(re.Message);
            }
        }
    });
}

//样式
function gridSize(){
    $('#jgTable02').setGridWidth($('.u_tabs_container').width() - 30);
}
